package com.quirqy.service;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import com.quirqy.model.Person;
import com.quirqy.model.Property;
import com.quirqy.model.PropertyType;
import com.quirqy.model.PropertyValue;

import com.quirqy.persistence.QuirqyPersistence;

@Controller
public class RestService 
{

	@RequestMapping( value = "people",method = RequestMethod.GET )
	@ResponseBody
	public final List< Person > getAllPeople()
	{
		return QuirqyPersistence.getAll("select p from Person p ");
	}

	@RequestMapping( value = "people/{id}/properties",method = RequestMethod.GET )
	@ResponseBody
	public final List <PropertyValue> getPersonProperties( @PathVariable( "id" ) final Long id )
	{


		String sQuery = String.format("select p from PropertyValue as p where p.person_id = %d", id);
		List <PropertyValue> ppVal = QuirqyPersistence.getAll(sQuery);
		for (PropertyValue p : ppVal)
		{
			//need to get propertyname and propertytype.
	    	Property pHolder = (Property)QuirqyPersistence.get(Property.class, p.getPropertyId());
	    	p.setPropertyName(pHolder.getName());
	    	p.setPropertyType(pHolder.getPropertyType());
	    }

		return ppVal;
	}
	
	@RequestMapping( value = "people/{id}/properties/{propId}",method = RequestMethod.GET )
	@ResponseBody
	public final List <PropertyValue> getPersonProperties( @PathVariable( "id" ) final Long id,
 														   @PathVariable( "propId" ) final Long propId )
	{


		String sQuery = String.format("select p from PropertyValue as p where p.person_id = %d and p.property_id=%d", id, propId);
		List <PropertyValue> ppVal = QuirqyPersistence.getAll(sQuery);
		for (PropertyValue p : ppVal)
		{
			//need to get propertyname and propertytype.
	    	Property pHolder = (Property)QuirqyPersistence.get(Property.class, p.getPropertyId());
	    	p.setPropertyName(pHolder.getName());
	    	p.setPropertyType(pHolder.getPropertyType());
	    }

		return ppVal;
	}

	@RequestMapping( value = "people/{id}",method = RequestMethod.GET )
	@ResponseBody
	public final Person getPerson( @PathVariable( "id" ) final Long id )
	{
		return (Person)QuirqyPersistence.get(Person.class, id);
	}
	
	@RequestMapping( value = "people/{id}/properties",method = RequestMethod.POST )
	@ResponseStatus( HttpStatus.CREATED )
	@ResponseBody	
	public final PropertyValue addProperty( @PathVariable( "id" ) final Long id,
											@RequestBody final PropertyValue p )
	{
		p.setId(null);
		p.setPersonId(id);
		return (PropertyValue)QuirqyPersistence.add(p);
	}
	
	@RequestMapping( value = "property",method = RequestMethod.GET )
	@ResponseBody
	public final List< Property > getAllProperty()
	{
		return QuirqyPersistence.getAll("select p from Property p ");
	}
	@RequestMapping( value = "property/{id}",method = RequestMethod.GET )
	@ResponseBody
	public final Property getProperty( @PathVariable( "id" ) final Long id )
	{
		return (Property)QuirqyPersistence.get(Property.class, id);
	}

	
	@RequestMapping( value = "people",method = RequestMethod.POST )
	@ResponseStatus( HttpStatus.CREATED )
	@ResponseBody	
	public final Person addPerson( @RequestBody final Person p )
	{
		//This seems wrong. I have to nullify the id to have it auto-generated?
		//If the other option is to create a copy of the incoming peron, then I give up. That is ridiculous.
		p.setId(null);
		return (Person)QuirqyPersistence.add(p);
	}
	
	@RequestMapping( value = "property",method = RequestMethod.POST )
	@ResponseStatus( HttpStatus.CREATED )
	@ResponseBody	
	public final Property addProperty( @RequestBody final Property p )
	{
		//This seems wrong. I have to nullify the id to have it auto-generated?
		//If the other option is to create a copy of the incoming peron, then I give up. That is ridiculous.
		p.setId(null);
		return (Property)QuirqyPersistence.add(p);
	}
	
	@RequestMapping( value = "people/{id}",method = RequestMethod.PUT )
	@ResponseStatus( HttpStatus.OK )
	@ResponseBody	
	public final Person setPerson( @PathVariable( "id" ) final Long id, @RequestBody final Person p )
	{
		return (Person)QuirqyPersistence.set(Person.class, id, p);
	}
	@RequestMapping( value = "property/{id}",method = RequestMethod.PUT )
	@ResponseStatus( HttpStatus.OK )
	@ResponseBody	
	public final Property setProperty( @PathVariable( "id" ) final Long id, @RequestBody final Property p )
	{
		p.setId(id);
		return (Property)QuirqyPersistence.set(Property.class, id, p);
	}
	
	@RequestMapping( value = "people/{id}",method = RequestMethod.DELETE )
	@ResponseStatus( HttpStatus.OK )
	public final void deletePerson( @PathVariable( "id" ) final Long id )
	{
		QuirqyPersistence.delete(Person.class, id);		
	}
	
	@RequestMapping( value = "property/{id}",method = RequestMethod.DELETE )
	@ResponseStatus( HttpStatus.OK )
	public final void deleteProperty( @PathVariable( "id" ) final Long id )
	{
		QuirqyPersistence.delete(Property.class, id);		
	}


/*	@RequestMapping( value = "people",method = RequestMethod.GET )
	@ResponseBody
	public final List< Person > getAll()
	{
		EntityManager em = EMF.get().createEntityManager();
		return em.createQuery("select p from Person p ").getResultList();
	}

	@RequestMapping( value = "people/{id}",method = RequestMethod.GET )
	@ResponseBody
	public final Person get( @PathVariable( "id" ) final Long id )
	{
		EntityManager em = EMF.get().createEntityManager();
		return em.find(Person.class, id);
	}
	*/
}
